package top.stmo.spring.ioc.test1;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.stmo.spring.ioc.test1.dao.BookDao;
import top.stmo.spring.ioc.test1.service.BookService;

import javax.sql.DataSource;

/**
 *   -------------------------总结--------------------------
 *
 *   容器相关
 *      BeanFactory是IoC容器的顶层接口，初始化BeanFactory对象时，加载的bean延迟加载
 *      ApplicationContext接口是Spring容器的核心接口，初始化时bean立即加载
 *      ApplicationContext接口提供基础的bean操作相关方法，通过其他接口扩展其功能
 *      ApplicationContext接口常用初始化类
 *      ClassPathXmlApplicationContext(常用)
 *      FileSystemXmlApplicationContext
 *
 *   bean相关
 *      最常用的就两个属性id和class
 *
 */
public class AppTest3 {

    /**
     *      依赖注入
     */
    @Test
    public void test1() {
        ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) act.getBean("bookService");
        bookService.save();
        System.out.println(bookService.toString());
    }
    /**
     *      Properties数据源对象的管理
     */
    @Test
    public void test2() {
        ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        DataSource dataSource = (DataSource) act.getBean("dataSource");
        System.out.println(dataSource);
    }

    @Test
    public void test3() {
        ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        ComboPooledDataSource c3p0 = (ComboPooledDataSource) act.getBean("c3p0");
        System.out.println(c3p0);
    }

    @Test
    public void test4() {
        ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao = (BookDao) act.getBean("bookDao1");
        bookDao.aaa();
    }

    @Test
    public void test5() {
        //加载类路径下的配置文件
        ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao = (BookDao) act.getBean("bookDao1");
        BookDao bookDao1 = act.getBean("bookDao1", BookDao.class);
        BookService bookSer2 = act.getBean(BookService.class);
        bookDao.aaa();
        bookDao1.aaa();
        bookSer2.save();
    }
}
